using System;
using System.Windows.Forms;
using SnarlGMailNotifier.Properties;

namespace SnarlGMailNotifier.Forms
{
    public partial class FEnterCredentials : Form
    {
        public FEnterCredentials()
        {
            InitializeComponent();
        }

        private void FEnterCredentials_Load(object sender, EventArgs e)
        {
            txtUsername.Text = Settings.Default.GMailUsername.Trim();
            if (txtUsername.Text.Length != 0)
            {
                txtPassword.TabIndex = 0;
                txtUsername.TabIndex = 5;
            }
            chkSavePwd.Checked = Settings.Default.SavePassword;
            this.Icon = Properties.Resources.gmail_icon;
        }

        public string Username
        {
            get
            {
                return this.txtUsername.Text;
            }
        }

        public string Password
        {
            get
            {
                return this.txtPassword.Text;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;

            if (!FNotify.IsValidEmail(txtUsername.Text))
            {
                txtUsername.BackColor = System.Drawing.Color.LightCoral;
                return;
            }

            #region save credentials
            Settings.Default.SavePassword = chkSavePwd.Checked;
            Settings.Default.GMailUsername = txtUsername.Text.Trim();

            if (chkSavePwd.Checked)
            {
                Settings.Default.GMailPassword = txtPassword.Text.Trim();
            }
            else
            {
                Settings.Default.GMailPassword = "";
            }

            Settings.Default.Save();
            #endregion
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}